/* BeerScreen, Copyright (C) 2010  Gabriel Tremblay <initnull@gmail.com>
 * http://www.etrange.ca/beerscreen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ca.etrange.beerscreen.view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.BorderFactory;
import javax.swing.JPanel;


/**
 * Abstract Panel with borders and variable transparency background
 * @author initnull
 * 
 */
public abstract class AbstractTranslucidPanel extends JPanel {
	private static final long serialVersionUID = -6312629092435434236L;
	private float backgroundTransparency = 0.55f;
	private int borderPixels = 2;
	private Color backgroundColor = Color.BLACK;

	public AbstractTranslucidPanel(){
		super();
		setOpaque(false);
		setBorderColor(Color.WHITE);
	}
	
	@Override
	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D)g;
		
		g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                (RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB));
		
		final Composite backupComposite = g2d.getComposite();
		
		AlphaComposite alpha = AlphaComposite.getInstance(
				AlphaComposite.SRC_OVER, backgroundTransparency);
		
		g2d.setComposite(alpha);
		g2d.setColor(backgroundColor);

		g2d.fillRect(
				0, 
				0, 
				this.getWidth() - borderPixels, 
				this.getHeight() - borderPixels);
		

		g2d.setComposite(backupComposite);
		super.paint(g2d);
		g2d.dispose();
	}

	public int getBorderPixels() {
		return borderPixels;
	}

	public void setBorderPixels(int borderPixels) {
		this.borderPixels = borderPixels;
	}

	public Color getBackgroundColor() {
		return backgroundColor;
	}

	public void setBackgroundColor(Color backgroundColor) {
		this.backgroundColor = backgroundColor;
	}
	
	public void setBorderColor(Color borderColor) {
		this.setBorder(
				BorderFactory.createLineBorder(borderColor, borderPixels));
	}
	
	public float getBackgroundTransparency() {
		return backgroundTransparency;
	}
	
	public void setBackgroundTransparency(float backgroundTransparency) {
		this.backgroundTransparency = backgroundTransparency;
	}
	
}
